package com.henghai.zhenxuan.ui.page.mall;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.ProductBean;
import com.henghai.zhenxuan.domain.request.AppListRequester;
import com.henghai.zhenxuan.ui.page.adapter.ProductListAdapter;
import com.henghai.zhenxuan.R;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;

import java.util.ArrayList;

public class MallListFragment extends BaseFragment {
    private MallStates mStates;
    private ProductListAdapter mAdapter;
    private AppListRequester mAppListRequester;
    private String productType = "";

    public MallListFragment(String productType) {
        this.productType = productType;
    }

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(MallStates.class);
        mAppListRequester = getFragmentScopeViewModel(AppListRequester.class);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        RecyclerView recyclerView = view.findViewById(R.id.mall_product_rv);
//        WaitDialog.show("正在加载");
        mAppListRequester.getAppListResult().observe(this, httpBeanDataResult -> {
//            WaitDialog.dismiss();
            if (!httpBeanDataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(httpBeanDataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            mStates.appList.set(httpBeanDataResult.getResult().getData().getRows());
            if (recyclerView != null) {
                recyclerView.post(() -> {
                    recyclerView.setNestedScrollingEnabled(true);
                });
            }
        });
        mAppListRequester.request(getViewLifecycleOwner(), productType, "1");
    }


    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new ProductListAdapter(getContext());
        mAdapter.setOnItemClickListener((i, productBean, i1) -> {
            Bundle bundle = new Bundle();
            bundle.putString(CommodityFragment.KEY, productBean.getId() + "");
            nav().navigate(com.henghai.zhenxuan.R.id.action_mainFragment_to_Commodity, bundle);
        });
        return new DataBindingConfig(R.layout.fragment_mall_list, BR.vm, mStates).addBindingParam(BR.adapter, mAdapter);
    }

    public static class MallStates extends StateHolder {
        public final State<ArrayList<ProductBean.RowsBean>> appList = new State<>(new ArrayList<>());
    }

}
